const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');
const Weather = require('./Weather');

const Forecast = sequelize.define('Forecast', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  weatherId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'weather',
      key: 'id'
    }
  },
  date: {
    type: DataTypes.DATE,
    allowNull: false
  },
  sunrise: {
    type: DataTypes.STRING,
    allowNull: true
  },
  sunset: {
    type: DataTypes.STRING,
    allowNull: true
  },
  temperatureHigh: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  temperatureLow: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  weatherCondition: {
    type: DataTypes.ENUM(
      '晴', '多云', '阴', '小雨', '中雨', '大雨', '暴雨', 
      '雷阵雨', '小雪', '中雪', '大雪', '雾', '霾'
    ),
    allowNull: true
  },
  weatherConditionNight: {
    type: DataTypes.ENUM(
      '晴', '多云', '阴', '小雨', '中雨', '大雨', '暴雨', 
      '雷阵雨', '小雪', '中雪', '大雪', '雾', '霾'
    ),
    allowNull: true
  },
  textDay: {
    type: DataTypes.STRING,
    allowNull: true
  },
  textNight: {
    type: DataTypes.STRING,
    allowNull: true
  },
  windSpeed: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  windDirection: {
    type: DataTypes.STRING,
    allowNull: true
  },
  windScale: {
    type: DataTypes.STRING,
    allowNull: true
  },
  windSpeedNight: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  windDirectionNight: {
    type: DataTypes.STRING,
    allowNull: true
  },
  windScaleNight: {
    type: DataTypes.STRING,
    allowNull: true
  },
  humidity: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  precipitation: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  pressure: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  visibility: {
    type: DataTypes.FLOAT,
    allowNull: true
  },
  uvIndex: {
    type: DataTypes.STRING,
    allowNull: true
  },
  probability: {
    type: DataTypes.FLOAT,
    allowNull: true,
    validate: {
      min: 0,
      max: 100
    }
  },
  createdAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  },
  updatedAt: {
    type: DataTypes.DATE,
    defaultValue: DataTypes.NOW
  }
}, {
  tableName: 'forecasts',
  timestamps: true,
  indexes: [
    {
      fields: ['weatherId']
    },
    {
      fields: ['date']
    }
  ]
});

// 建立关联关系
Weather.hasMany(Forecast, { foreignKey: 'weatherId' });
Forecast.belongsTo(Weather, { foreignKey: 'weatherId' });

module.exports = Forecast; 